package com.hkts.naturalgasauthentication.management.service.impl;

import com.hkts.naturalgasauthentication.management.dao.WebsiteInformationDao;
import com.hkts.naturalgasauthentication.management.dao.WebsiteParamsDao;
import com.hkts.naturalgasauthentication.management.entity.WebsiteInformation;
import com.hkts.naturalgasauthentication.management.service.WebsiteInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameWebsiteInformationServiceImpl
 * @Description
 * @Author yck
 * @Date2021-02-18 15:47
 * @Version V1.0
 **/
@Service
@Transactional
public class WebsiteInformationServiceImpl implements WebsiteInformationService {
    @Autowired
    private WebsiteInformationDao websiteInformationDao;
    @Autowired
    private WebsiteParamsDao websiteParamsDaoImpl;

    @Override
    public void saveWebsiteInformation(WebsiteInformation websiteInformation) {
        websiteInformationDao.save(websiteInformation);
    }

    @Override
    public void updateWebsiteInformation(WebsiteInformation websiteInformation) {
        websiteInformationDao.save(websiteInformation);
    }

    @Override
    public void deleteWebsiteInformation(String id) {
        websiteInformationDao.deleteById(id);
        websiteParamsDaoImpl.deleteWebsiteParamsByWebsiteId(id);
    }

    @Override
    public Page<WebsiteInformation> getWebsiteInformationPage(Pageable pageable, WebsiteInformation websiteInformation) {


        Page<WebsiteInformation> all = websiteInformationDao.findAll(new Specification<WebsiteInformation>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<WebsiteInformation> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                /*Path<Date> createTimeField = root.get("createTime");*/

                List<Predicate> list = new ArrayList<Predicate>();

                // 创建时间
               /* if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }*/

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);


       return  all;

    }

    @Override
    public WebsiteInformation getWebsiteInformationById(String id) {
        WebsiteInformation one = websiteInformationDao.getOne(id);
        return one;
    }

    @Override
    public List<WebsiteInformation> getWebsiteInformationAll() {
        Sort websiteOrder = Sort.by("websiteOrder");
        List<WebsiteInformation> all = websiteInformationDao.findAll(websiteOrder);
        return all;
    }
}
